package abg.core.common.util;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@RestController
public class FileDownloadController {

    @GetMapping("/downloadFiles")
    public ResponseEntity<byte[]> downloadFiles() {
        try {
            // 创建 PipedInputStream 和 PipedOutputStream 对象
            PipedOutputStream pipedOutputStream = new PipedOutputStream();
            final PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);

            // 开始文件下载处理
            try {
                // 模拟要打包的多个文件（假设这里有文件内容列表）
                String[] fileContents = {"Content of file 1", "Content of file 2", "Content of file 3"};

                // 创建内存中的输出流
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);

                // 打包每个文件并写入压缩流
                for (int i = 0; i < fileContents.length; i++) {
                    String fileName = "file" + (i + 1) + ".txt";

                    // 将文件内容写入临时压缩流
                    ByteArrayOutputStream tempByteArrayOutputStream = new ByteArrayOutputStream();
                    ZipOutputStream tempZipOutputStream = new ZipOutputStream(tempByteArrayOutputStream);

                    ZipEntry zipEntry = new ZipEntry(fileName);
                    tempZipOutputStream.putNextEntry(zipEntry);
                    tempZipOutputStream.write(fileContents[i].getBytes());
                    tempZipOutputStream.closeEntry();
                    tempZipOutputStream.finish();
                    tempZipOutputStream.close();

                    // 将临时压缩流内容写入主压缩流
                    ZipEntry entry = new ZipEntry(fileName + ".zip");
                    zipOutputStream.putNextEntry(entry);
                    zipOutputStream.write(tempByteArrayOutputStream.toByteArray());
                    zipOutputStream.closeEntry();
                }

                // 关闭主压缩流
                zipOutputStream.finish();
                zipOutputStream.close();

                // 将压缩后的数据写入管道输出流
                pipedOutputStream.write(byteArrayOutputStream.toByteArray());
                pipedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 构建响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", "files.zip");

            // 从管道输入流中读取数据并构建响应实体
            byte[] responseBytes = new byte[pipedInputStream.available()];
            pipedInputStream.read(responseBytes);
            ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(responseBytes, headers, HttpStatus.OK);

            return responseEntity;
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
}
